package com.ithema.reggie.controller;

import com.ithema.reggie.common.R;
import com.ithema.reggie.entity.ShoppingCart;
import com.ithema.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("add")
    public R add2Cart(@RequestBody ShoppingCart paramCart, HttpSession session){
        //1给购物车设置id
        Long userId = (Long) session.getAttribute("user");
        paramCart.setUserId(userId);

        //2.调用service实现购物车添加
        return shoppingCartService.add2Cart(paramCart);
    }

    /**
     *
     * 作用： 查看购物车
     * @param session
     * @return
     */
    @GetMapping("list")
    public R showCart(HttpSession session){

        //1.获取当前用户登录的id
        Long userId = (Long) session.getAttribute("user");

        //2.调用service实现查看操作
        return shoppingCartService.showCart(userId);
    }

    @DeleteMapping("clean")
    public R cleanCart(HttpSession session){
        //1.获取用户的id
        Long userId = (Long) session.getAttribute("user");
        //2.调用service清空购物车
        return shoppingCartService.cleanCart(userId);
    }
}
